package com.sservice.service.utils.wx_third_platform;

import com.sservice.common.Constants;
import com.sservice.service.utils.wx_third_platform.aes.AesException;
import com.sservice.service.utils.wx_third_platform.aes.WXBizMsgCrypt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;

/**
 * Created by suweiming on 2018/8/19.
 */
public class WxAes {
    private static Logger logger = LoggerFactory.getLogger(WxAes.class);

    public static void main(String[] args) throws Exception {
        test();
    }


    public static String encryptMsg(String timestamp, String nonce, String replyMsg) throws AesException {
        // 需要加密的明文
        String encodingAesKey = Constants.wxThirdPlatformKey;
        String token = Constants.wxThirdPlatformToken;
        String appId = Constants.componentAppId;
        WXBizMsgCrypt pc = new WXBizMsgCrypt(token, encodingAesKey, appId);
        String mingwen = pc.encryptMsg(replyMsg, timestamp, nonce);
        System.out.println("===========加密前: " + replyMsg);
        System.out.println("===========加密后: " + mingwen);
        return mingwen;
    }

    public static String decryptMsg(String postData, String timestamp, String nonce, String msg_signature) throws AesException, ParserConfigurationException, IOException, SAXException {
        String encodingAesKey = Constants.wxThirdPlatformKey;
        String token = Constants.wxThirdPlatformToken;
        String appId = Constants.componentAppId;
        WXBizMsgCrypt pc = new WXBizMsgCrypt(token, encodingAesKey, appId);
        String result2 = pc.decryptMsg(msg_signature, timestamp, nonce, postData);
        return result2;
    }

    public static void test() throws AesException, ParserConfigurationException, IOException, SAXException {

        //
        // 第三方回复公众平台
        //

        // 需要加密的明文
        String encodingAesKey = "5pPtTkMJ0zL5TGehfEPAnzkZcIswYZdXaSqTP8k29on";
        String token = "ZS8XbXmExcVpW%%CG^@b@@";
        String timestamp = "1535980112";
        String nonce = "1914821370";
        String msgSignature = "982e1e41f866087f675a765e6e54f78db8f0f38f";
        String appId = "wxcd9a32054b84abd8";
        String mingwen = "<xml>\n" +
                "    <AppId><![CDATA[wxcd9a32054b84abd8]]></AppId>\n" +
                "    <Encrypt><![CDATA[tMhSrqiTswG3KGx/vvWV0BUvbGDgsM4czRH9Nxg58w45MqU2RL9ootvEXD1qEnNYqC5R952B6cTCT1fr/GU2FjhyCcKvq8gu1FljMbrg+ot2vmm3sDRfI7cbSwu9mkxJK37JyGRjed86Xr6u8gTT4VMUQZKUmSvF5/4Xcly8aIHIZ3NY9N+OzYbkA0vbosOwCEiP++GCM+Nhy+dAi0itr1NVNkR1eakkf00VU5j5CnH4xVy56KaR4uIKpbFy1qMM9Sis0bHIS/xdM3nrM+PHuu3eZyMzjrQrZxNYQTAoh6SzNi8K9dXiG3owJqGrDKb3aYJUbn+yBt/GETR7e/hx1+Rgwz4NTMKv03EZmy5f7/orxdf5EXTJtYWHMvM8OkB9WnFWtFmKF8xGYvzqhjFO8XhMwJpjaf0P1RhjTu5xipO63gfkGvv+I2HBhAIQPE3DhJfQviV9mEYGlrjEBkwV4w==]]></Encrypt>\n" +
                "</xml>\n";
        WXBizMsgCrypt pc = new WXBizMsgCrypt(token, encodingAesKey, appId);


        String result2 = pc.decryptMsg(msgSignature, timestamp, nonce, mingwen);
        System.out.println("解密后明文: " + result2);

        //ComponentVerifyTicketPostData componentVerifyTicketPostData  = WxPayBaseResult.fromXML(result2, ComponentVerifyTicketPostData.class);
        //System.out.println("解密后明文: " + componentVerifyTicketPostData.getComponentVerifyTicket());
        //pc.verifyUrl(null, null, null, null);
    }


}
